'use strict';//管理员增删改查
const Controller = require('egg').Controller;
const utility = require("utility")//密码加密
class AdminController extends Controller {
    async index() { //模糊搜索跟分页查询
        const { ctx, app } = this;
        const { keyname, page = 1, number = 5 } = ctx.request.query
        const offset = (page - 1) * number
        let data, count
        if (keyname && page) {
            data = await app.mysql.query(`select id,user_name,password,name from admin_name where user_name like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from admin_name where user_name like '%${keyname}%'`)
        } else if (keyname) {
            data = await app.mysql.query(`select id,user_name,password,name from admin_name where user_name ='%${keyname}%' or user_name  like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from admin_name where user_name like '%${keyname}%' or user_name like '%${keyname}%'`)
        } else {
            data = await app.mysql.query(`select id,user_name,password,name from admin_name limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from admin_name`)
        }
        ctx.body = {
            code: 0,
            msg: "查找成功",
            data,
            count: count
        }
    }

    async create() {
        const { ctx, app } = this;
        const { user_name, password, name } = ctx.request.body;

        const result = await app.mysql.insert('admin_name', { user_name, name, password: utility.md5(password) })
        if (user_name && password && name) {
            ctx.body = {
                code: 0,
                msg: "添加成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 406,
                msg: "添加失败"
            };
        }
    }
    async update() {
        const { ctx, app } = this;
        const { password, name } = ctx.request.body;
        const { id } = ctx.params;
        const row = {
            name: name,
            password: password
        }
        const options = {
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('admin_name', row, options)
        if (result) {
            ctx.body = {
                code: 0,
                msg: "修改成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 408,
                msg: "修改失败",
            };
        }
    }

    async destroy() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        const result = await app.mysql.delete(`admin_name`, { id: +id })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 407,
                msg: "删除失败"
            };
        }
    }
}
module.exports = AdminController;
